const fs = require('fs')
const path = require('path')
const express = require('express')
const server = express()
const opn = require('opn')
server.use(express.static('dist'))
const createApp = require(path.resolve(__dirname, 'dist/server.js'))

const bundle = fs.readFileSync(path.resolve(__dirname, 'dist/server.js'), 'utf-8')
const renderer = require('vue-server-renderer').createBundleRenderer(bundle, {
  template: fs.readFileSync(path.resolve(__dirname, 'dist/index.ssr.html'), 'utf-8')
})

server.get('/index', (req, res) => {
  const app = createApp()
  renderer.renderToString(app, (err, html) => {
    if (err) {
      console.error(err)
      res.status(500).end('服务器内部错误')
      return
    }
    res.end(html)
  })
})

let port = 8002
server.listen(port, () => {
  let url = `http://localhost:${port}`
  console.log(`后端渲染服务器启动：${url}`)
  opn(`${url}/index`)
})
//
// const feServer = express()
// feServer.use(express.static('dist'))
//
// feServer.get('/index', (req, res) => {
//   // 输出 html
//   let html = fs.readFileSync(path.resolve(__dirname, 'dist/index.html'), 'utf-8')
//   res.end(html)
// })
//
// feServer.listen(8003, () => {
//   console.log('前端渲染服务器启动，端口号为：8003')
// })
